package com.netease.nim.demo.session.model.extension;

import com.alibaba.fastjson.JSONObject;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;

/**
 * Created by zhoujianghua on 2015/4/9.
 */
public abstract class CustomAttachment implements MsgAttachment {

    protected int type;

    CustomAttachment(int type) {
        this.type = type;
    }

    public void fromJson(JSONObject json) {
        type = json.getInteger("type");
        JSONObject data = json.getJSONObject("data");
        if (data != null) {
            parseData(data);
        }
    }

    @Override
    public String toJson(boolean send) {
        JSONObject object = new JSONObject();
        object.put("type", type);
        JSONObject data = packData();
        if (data != null) {
            object.put("data", data);
        }

        return object.toJSONString();
    }

    public int getType() {
        return type;
    }

    protected abstract void parseData(JSONObject data);
    protected abstract JSONObject packData();
}
